﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sgr.BillNumbering.Domain;
using Sgr.EntityFrameworkCore.EntityConfigurations;

namespace Sgr.BillNumbering.Infrastructure.EntityConfigurations
{
    public class SequenceCounterConfiguration : IEntityTypeConfiguration<SequenceCounter>
    {
        public void Configure(EntityTypeBuilder<SequenceCounter> builder)
        {
            builder.ToTable("sgr_sequence_counter");

            builder.HasKey(e => new { e.Category, e.TimeKey, e.CustomPrefix });

            builder.Property(e => e.Category)
                .HasColumnName("sgr_category")
                .HasMaxLength(32)
                .IsRequired();

            builder.Property(e => e.TimeKey)
                .HasColumnName("sgr_timekey")
                .HasMaxLength(16)
                .IsRequired();

            builder.Property(e => e.CustomPrefix)
                .HasColumnName("sgr_custom_prefix")
                .HasMaxLength(64); // 可选

            builder.Property(e => e.MaxValue)
                .HasColumnName("sgr_maxvalue")
                .IsRequired();

            builder.Property(e => e.AllocatedMaxValue)
                .HasColumnName("sgr_allocated_maxvalue")
                .IsRequired();

            builder.Property(e => e.UpdatedAt)
                .HasColumnName("sgr_update_at")
                .IsRequired();

            builder.Property(e => e.ExpiryTime)
                .HasColumnName("sgr_expriry");

            builder.Property(e => e.RowVersion)
                .HasColumnName("sgr_row_version")
                .IsConcurrencyToken()
                .IsRequired();
        }
    }
}